package com.bcmusic.music.controller;

import com.bcmusic.music.config.TokenDecode;
import com.bcmusic.music.domain.permission.dto.AddPermissionDTO;
import com.bcmusic.music.domain.permission.vo.MarkerOfMusicVO;
import com.bcmusic.music.domain.permission.vo.PerMissionDetailVO;
import com.bcmusic.music.pojo.MusicPermission;
import com.bcmusic.music.service.MusicPermissionService;
import com.github.pagehelper.PageInfo;
import entity.AllowType;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.*;
import org.elasticsearch.cluster.routing.allocation.RerouteExplanation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/****
 * @Author:admin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(tags = "音乐版权模块")
@RestController
@RequestMapping("/musicPermission")
@CrossOrigin
public class MusicPermissionController {

    @Autowired
    private MusicPermissionService musicPermissionService;

    @Autowired
    private TokenDecode tokenDecode;

    /***
     * 根据ID查询MusicPermission数据
     * @param id
     * @return
     */
    @ApiIgnore
    @GetMapping("getPermissionById")
    public Result<MusicPermission> getPermissionById(@RequestParam Integer id){
        //调用MusicPermissionService实现根据主键查询MusicPermission
        MusicPermission musicPermission = musicPermissionService.findById(id);
        return new Result<MusicPermission>(true,StatusCode.OK,"查询成功",musicPermission);
    }

    /**
     * 校验购物车数据合法性时候调用
     * @param idList
     * @return
     */
    @ApiIgnore
    @GetMapping("getPermissionByIdList")
    public Result<List<MusicPermission>>getPermissionByIdList(@RequestParam List<Integer> idList){
        //批量查询的顺序搞不定
        ArrayList<MusicPermission>arrayList=new ArrayList<>();
        for (int i = 0; i < idList.size(); i++) {
            MusicPermission byId = musicPermissionService.findById(idList.get(i));
            arrayList.add(byId);
        }
        return new Result<>(true,StatusCode.ERROR,"查询成功",arrayList);
    }

    @ApiOperation(value = "发布许可",notes = "发布许可",tags = {"MusicPermissionController"})
    @PostMapping("addPermission")
    public Result addPermission(@RequestBody AddPermissionDTO addPermissionDTO){
        String userNum=tokenDecode.getUserInfo().get("id");
        int i = musicPermissionService.addPermission(addPermissionDTO,userNum);
        return i==1?new Result(true,StatusCode.OK,"发布成功"):new Result(true,StatusCode.OK,"发布失败");
    }

    /**
     * 检查该音乐是否可以发布（目前是许可）
     * @param musicId
     * @param type
     * @return
     */
    @GetMapping("check")
    @ApiOperation(value = "查询是否可以发布（许可，转让给，授权）",notes = "查询是否可以发布（许可，转让给，授权）",tags = {"MusicPermissionController"})
    public Result<Boolean> check(@RequestParam @ApiParam(name = "musicId",value = "音乐id",required = true)Integer musicId,@ApiParam(name = "type",value = "授权类型，0许可，1转让，2分发",required = true)@RequestParam Integer type){
        int check = musicPermissionService.check(musicId, type);
        if(check==1){
            return new Result(true,StatusCode.OK,"允许发布",true);
        }else {
            return new Result<>(true,StatusCode.ERROR,"请撤销历史记录再发布",false);
        }
    }

    /**
     * 撤销
     * @param backOutList
     * @param musicId
     * @return
     */
    @ApiOperation(value = "撤销发布（许可，转让给，授权）",notes = "撤销发布（许可，转让给，授权）",tags = {"MusicPermissionController"})
    @PostMapping("backOut")
    public Result backOut(@RequestParam @ApiParam(name = "backOutList",value = "撤销数据列表（List<Integer>），长度为3，第0项表示许可，第1项表示转让，第二项表示分发，如果为1就撤销，为0则不撤销",required = true)List<Integer> backOutList,
                          @RequestParam @ApiParam(name = "musicId",value = "音乐id",required = true)Integer musicId){
        String userNum=tokenDecode.getUserInfo().get("id");
        int i = musicPermissionService.backOut(musicId, backOutList, userNum);
        return i==1?new Result(true,StatusCode.OK,"撤销成功"):new Result(true,StatusCode.OK,"撤销失败");
    }

    @ApiOperation(value = "获取音乐市场分页列表",notes = "获取音乐市场分页列表",tags = {"MusicPermissionController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping("getMarketOfMusic/{page}/{size}")
    public Result<List<MarkerOfMusicVO>> getMarketOfMusic(@PathVariable("page") Integer page,@PathVariable("size")Integer size){
        List<MarkerOfMusicVO> marketOfMusic = musicPermissionService.getMarketOfMusic(page, size);
        return new Result<>(true,StatusCode.OK,"查询成功",marketOfMusic);
    }

    @ApiOperation(value = "获取可购买的授权信息",notes = "获取可购买的授权信息",tags = {"MusicPermissionController"})
    @GetMapping("getMusicPermissionDetail")
    public Result<PerMissionDetailVO> getMusicPermissionDetail(@RequestParam("type") @ApiParam("该音乐类型，0为许可，1为转让，2为分发") Integer type,@RequestParam("musicId") @ApiParam("音乐id") Integer musicId){
        PerMissionDetailVO musicPermissionDetail = musicPermissionService.getMusicPermissionDetail(type, musicId);
        if(musicPermissionDetail==null){
            return new Result(true,StatusCode.ERROR,"该商品已下架，请刷新列表重试");
        }
        return new Result(true,StatusCode.OK,"查询成功",musicPermissionDetail);
    }

}
